<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 深拷贝的两种方式
        // 1.josn格式
        const arr = [1, 5, 6, 54, 3, 6, 2, 6]
        function deepClone(arr) {
            const obj = JSON.stringify(arr)
            const newarr = JSON.parse(obj)
            return newarr
        }
        deepClone(arr)
        console.log(deepClone(arr))

        // 2.遍历深拷贝
        var obj = {
            name: '胖鸡',
            age: 19,
            num: 30
        }
        function deepclone(obj) {
            const newobj = {}
            for (key in obj) {
                newobj[key] = obj[key]
            }
            return newobj
        }
        deepclone(obj)
        console.log(deepclone(obj))

        // 随机排序
        // 1. 原生
        function sortarr(arr) {
            const newarr = [...arr]
            const length = newarr.length
            for (var i = 0; i < length; i++) {
                const index = Math.floor(Math.random() * length)
                const tem = newarr[index]
                newarr[index] = newarr[i]
                newarr[i] = tem
            }
            return newarr
        }
        sortarr(arr)
        console.log(arr)
        console.log(sortarr(arr))
        // sort方法
        function sortArr(arr) {
            const news = [...arr]
            news.sort((a, b) => {
                return Math.random() - 0.5
            })
            return news
        }
        console.log(arr)
        console.log(sortArr(arr))
    </script>
</body>

</html>